package cs236369.hw5;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet({"/admin/AdminTools", "/admin/AdminTools/*"})
public class AdminTools extends HttpServlet {
	private static final long serialVersionUID = -3140091183482070968L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		if(session == null) {
			response.sendRedirect(request.getContextPath() + InitListener.welcomePage);
			return;
		}
		Object isAdmin = session.getAttribute("admin");
		if(isAdmin == null) { // user is not an admin
			response.sendRedirect(request.getContextPath() + InitListener.welcomePage);
			return;
		}
		PrintWriter out;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			response.setStatus(400);
			return;
		}
		
		try {
			String command = request.getParameter("command");
			if(command == null) { response.setStatus(400); }
			else if(command.equals("deleteUser")) { delUser(request, response); }
			else if(command.equals("inverseSkill")) { updateSkillAvalibility(request, response); }
			else if(command.equals("addSkill")) { out.print(AddSkill(request, response)); }
			else if(command.equals("delMission")) { delMission(request, response); }
			else {
				response.setStatus(400);
				out.print("Illegal command");
			}
		} catch(SQLException e) {
			e.printStackTrace();
			response.setStatus(400);
			out.print("Internal DB error: " + e.getMessage());
		} catch(RescueMeException e) {
			response.setStatus(400);
			out.print(e.getMessage());
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	private void delUser(HttpServletRequest request, HttpServletResponse response) throws RescueMeException, SQLException {
		String username = request.getParameter("username");
		DBConnection.delUser(username);
	}
	
	private void updateSkillAvalibility(HttpServletRequest request, HttpServletResponse response) throws RescueMeException, SQLException {
		int id = Integer.parseInt(request.getParameter("id"));
		DBConnection.inverseSkillAvailability(id);
	}
	
	private int AddSkill(HttpServletRequest request, HttpServletResponse response) throws RescueMeException, SQLException {
		String skill = request.getParameter("skillName");
		return DBConnection.addSkill(skill);
	}
	
	private void delMission(HttpServletRequest request, HttpServletResponse response) throws RescueMeException, SQLException {
		int id;
		try {
			id = Integer.parseInt(request.getParameter("id"));
		} catch(NumberFormatException e) {
			throw new RescueMeException("illegal parameter", "id is not a legal number");
		}
		DBConnection.delPost(id, 0);
	}
}
